package cn.fy.sqlite.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import cn.fy.sqlite.databinding.ActivityMainBinding;
import cn.fy.sqlite.databinding.ActivityThreadBinding;

public class ThreadActivity extends AppCompatActivity {

    private ActivityThreadBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_thread);
        binding = ActivityThreadBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("线程");

        /**
         * 线程操作界面需要用Handler
         */
        binding.btnStartThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(() -> {
                    Log.i(getClass().getName(), "Thread start");
                    postMsg("Thread start");
                    try {
                        Thread.sleep(30*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i(getClass().getName(), "Thread end");
                    postMsg("Thread end");
                }).start();
            }
        });

        /**
         * 线程操作界面不用Handler出错
         */
        binding.btnStartThreadError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(() -> {
                    Log.i(getClass().getName(), "Thread start");
                    Toast.makeText(ThreadActivity.this, "Thread start", Toast.LENGTH_SHORT).show();
                    try {
                        Thread.sleep(30*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i(getClass().getName(), "Thread end");
                    Toast.makeText(ThreadActivity.this, "Thread end", Toast.LENGTH_SHORT).show();
                }).start();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void postMsg(String content) {
        Bundle bundle = new Bundle();
        bundle.putString("msg", content);
        Message msg = new Message();
        msg.setData(bundle);
        msgHandler.sendMessage(msg);
    }

    Handler msgHandler = new Handler(Looper.getMainLooper()) {

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            Bundle bundle = msg.getData();

            String content = bundle.getString("msg");

            Toast.makeText(ThreadActivity.this, content, Toast.LENGTH_SHORT).show();
        }
    };
}